/*
 *  Copyright (C) 2010 glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.defaults;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.balisunrise.application.Application;
import org.balisunrise.common.ActionCollection;

/**
 * Mantém e disponibiliza os eventos de uma entidade.
 *
 * @author Glauco
 */
public class DefaultActionCollection implements ActionCollection{

    // <editor-fold defaultstate="collapsed" desc="PropertyChangeSupport">
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    public static final String PROP_ENTITYCLASS = "entityClass";
    public static final String PROP_ACTION_MAPS = "actionMaps";

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    //</editor-fold>

    /**
     * Classe da entidade que os eventos representão.
     */
    private Class entityClass;

    /**
     * Mapa com as classes dos mapas de ações e as instâncias dos mesmos.
     */
    private Map<Class,Object> actionMaps;

    /**
     * Contrutor padrão.
     * @param entityClass
     * @param eventMaps
     */
    public DefaultActionCollection(Class entityClass, List<Class> actionMaps) {
        this.actionMaps = new HashMap<Class, Object>();
        this.entityClass = entityClass;
        for (Class c : actionMaps) {
            addActionMap(c);
        }
    }

    /**
     * Cria uma instancia para o mapa de ações a adiciona a lista.
     * @param actionMap mapa de ações.
     */
    public final void addActionMap(Class actionMap){

        if(actionMaps.containsKey(actionMap))
            return ;

        try{
            Object instance = actionMap.newInstance();
            actionMaps.put(actionMap, instance);
        }catch(Exception ex){
            Application.get().getLogger().log(Level.SEVERE,
                    "Não foi possível instânciar o ActionMap.", ex);
        }
    }

    /**
     * Remove da lista.
     * @param actionMap mapa de ações.
     */
    public final void removeActionMap(Class actionMap){

        if(actionMaps.containsKey(actionMap))
            actionMaps.remove(actionMap);
    }

    /**
     * Set the entityClass
     * @param entityClass
     */
    public void setEntityClass(Class entityClass) {
        Class old = this.entityClass;
        this.entityClass = entityClass;
        propertyChangeSupport.firePropertyChange(PROP_ENTITYCLASS,
                old, entityClass);
    }

    /**
     * Retorna a classe da entidade dos eventos.
     */
    @Override
    public Class getEntityClass() {
        return entityClass;
    }

    /**
     * Retorna os mapas de eventos da entidade;
     */
    @Override
    public Object[] getActionMaps() {
        return actionMaps.keySet().toArray(new Object[actionMaps.size()]);
    }

    /**
     * Retorna o objeto do mapa de ação da classe selecionada.
     * @param actionMapClass classe do mapa de ação.
     */
    @Override
    public Object getActionMap(Class actionMapClass) {
        if(actionMaps.containsKey(actionMapClass))
            return actionMaps.get(actionMapClass);
        return null;
    }

}
